package com.example.administrator.baikangxing;


import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.administrator.baikangxing.bean.Recorder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class VoiceListAdapter extends ArrayAdapter<Recorder> {

    private List<Recorder> mDatas;
    private Context context;

    private int minItemWidth;
    private int maxItemWidth;

    private LayoutInflater inflater;
    private TextView item_date;

    public VoiceListAdapter(Context context, List<Recorder> datas) {
        super(context, -1, datas);
        this.context = context;
        mDatas = datas;
        inflater = LayoutInflater.from(getContext());
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        maxItemWidth = (int) (outMetrics.widthPixels * 0.8);
        maxItemWidth = (int) (outMetrics.widthPixels * 0.2);
    }

    @Override
    public int getItemViewType(int position) {
        Recorder.Type type = mDatas.get(position).getType();
        if (type == Recorder.Type.comeVoice) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.list_item_cone_voice, parent, false);
                holder = new ViewHolder();
                holder.seconds = (TextView) convertView.findViewById(R.id.textLength);
                holder.length = convertView.findViewById(R.id.voiceAnim);
                holder.item_date = (TextView) convertView.findViewById(R.id.come_tv_date);
            } else {
                convertView = inflater.inflate(R.layout.list_item_voice, parent, false);
                holder = new ViewHolder();
                holder.seconds = (TextView) convertView.findViewById(R.id.textLength);
                holder.length = convertView.findViewById(R.id.voiceAnim);
                holder.item_date = (TextView) convertView.findViewById(R.id.out_tv_date);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.seconds.setText(Math.round(getItem(position).getAudioLength()) + "\"");
//		ViewGroup.LayoutParams params = holder.length.getLayoutParams();
//		params.width = (int) (minItemWidth + maxItemWidth / 60f
//				* getItem(position).audioLength);
//		holder.length.setLayoutParams(params);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(position == 0 || mDatas.get(position).getTime().getTime()-mDatas.get(position-1).getTime().getTime()>5*60*1000){
            holder.item_date.setText(df.format(mDatas.get(position).getTime()));
        }else {
            holder.item_date.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView seconds;
        View length;
        TextView item_date;
    }

}
